<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>寄生式组合继承</title>
</head>
<body>
    <script>
        // 为了解决组合继承的缺点，组合继承调用了两次父构造函数，
        // 生成了两个实例属性，只不过实例上的覆盖了原型上的属性。
        // 用了寄生式继承的方法，将子类原型指向父类原型，一般是Object.create() 与 Object.setPrototype()
        function objectCopy(obj) {
            function Fun() {};
            Fun.prototype = obj;
            return new Fun();
        };

        function inheritPrototype(child, parent) {
            let prototype = objectCopy(parent.prototype);  // 创建对象，这个对象是实例对象
            prototype.constructor = child; // 增强对象
            Child.prototype = prototype;  // 赋值对象
        };

        function Person(name) {
            this.name = name;
            this.firends = ['rose', 'lily', 'tom'];
        };

        Parent.prototype.sayName = function() {
            console.log(this.name);
        };

        function Child(name, age) {
            Parent.call(this, name);
            this.age = age;
        };

        inherit(Child, Parent);
        Child.prototype.sayName = function() {
            console.log(this.age);
        };

        let c1 = new Child('Katrina', 18);
        console.log(c1.sayName());  // 'Katrina'
    </script>
</body>
</html>